﻿#region 文件信息

// FileName: SoCafeShop/Sohobloo.SoAuth.Service/ActionService.cs
// Author: Sohobloo
// CreateTime: 2013/06/30 11:51
// UpdateTime: 2013/06/30 12:08

#endregion

#region 引用

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Sohobloo.SoAuth.Data.Repositories;
using Sohobloo.SoAuth.Entity;
using Sohobloo.SoMcCoffee.Public.OperationResult;

#endregion

namespace Sohobloo.SoAuth.Service.Impl
{
    [Export(typeof (IActionService))]
    internal class ActionService : ServiceBase, IActionService
    {
        #region 属性

        [Import]
        IActionRepository ActionRepository { get; set; }

        [Import]
        IFunctionRepository FunctionRepository { get; set; }

        [Import]
        IPermissionRepository PermissionRepository { get; set; }

        #endregion 属性

        #region 方法

        public OperationResult CreateAction(SoAuthAction action)
        {
            //检查功能名称重复
            if (ActionRepository.Entities.Any(a => a.ActionFunction.Id == action.ActionFunction.Id
                                                   && a.ActionName == action.ActionName))
            {
                return new OperationResult(OperationResultType.Conflict, "操作名称重复");
            }

            ActionRepository.Insert(action, false);

            return UnitOfWork.Commit() > 0
                       ? new OperationResult(OperationResultType.Success)
                       : new OperationResult(OperationResultType.NoChange);
        }

        public OperationResult DeleteAction(Guid actionId)
        {
            PermissionRepository.Delete(p => p.PermissionAction.Id == actionId, false);
            ActionRepository.Delete(a => a.Id == actionId, false);

            return UnitOfWork.Commit() > 0
                       ? new OperationResult(OperationResultType.Success)
                       : new OperationResult(OperationResultType.NoChange);
        }

        public OperationResult UpdateAction(SoAuthAction action)
        {
            if (ActionRepository.Entities.Any(a => a.ActionFunction.Id == action.ActionFunction.Id
                                                   && a.ActionName == action.ActionName
                                                   && a.Id != action.Id))
            {
                return new OperationResult(OperationResultType.Conflict, "操作名称重复");
            }

            ActionRepository.Update(action, false);

            return UnitOfWork.Commit() > 0
                       ? new OperationResult(OperationResultType.Success)
                       : new OperationResult(OperationResultType.Conflict);
        }

        public ICollection<SoAuthAction> GetActionList(Guid functionId)
        {
            return FunctionRepository.GetByKey(functionId).FunctionActions.OrderBy(a => a.CreateTime).ToList();
        }

        public SoAuthAction GetAction(Guid actionId)
        {
            return ActionRepository.GetByKey(actionId);
        }

        #endregion 方法
    }
}